class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :phone_number, :fax_number, :bio
  attr_accessor :specs
  has_many :fieldifications
  has_many :fields_of_work, :through => :fieldifications, :source => :field_of_work
  has_many :specializations
  has_many :specialities, :through => :specializations
  has_many :idea_invitations
  has_many :invited_ideas, :through => :idea_invitations, :source => :idea
  has_many :created_ideas, :class_name => "Idea", :foreign_key => "creator_id"
  has_many :participations
  has_many :participated_ideas, :through => :participations, :source => :idea
  has_many :comments
  
  
  def specs
    if specialities.present?
      specialities.map(&:name).to_json
    else
      ""
    end
  end
  
  def specs=(value)
    needs_names = value.split(',')
    self.specialities = needs_names.collect { |n| Speciality.find_or_create_by_name n }
  end
  
end
